//
//  HESpeechManager.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/23.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HESpeechManager.h"

@interface HESpeechManager ()<AVSpeechSynthesizerDelegate>


@property (nonatomic, strong) AVSpeechSynthesizer *avSpeaker;
@property (nonatomic, strong) AVSpeechUtterance *speechUtt;
@property (nonatomic, assign) BOOL nRepeat;



@end

@implementation HESpeechManager
 
#pragma mark - LifeCycle
+(instancetype)sharedManager {
    static dispatch_once_t onceToken;
    static HESpeechManager *instance;
    dispatch_once(&onceToken, ^{
        instance = [[HESpeechManager alloc] init];
        instance.canSpeaker = YES;
    });
    return instance;
}

- (void)enable:(BOOL)enable {
    if (!enable) {
        [self stopSpeech];
    }
    [HESpeechManager sharedManager].canSpeaker = enable;
}

- (void)startWithText:(NSString *)text {
    
    if (![HESpeechManager sharedManager].canSpeaker) {
        return;
    }
    
    [self stopSpeech];
    
    [self setAudioWaiFangSession];
    
    //初始化语音合成器
    _avSpeaker = [[AVSpeechSynthesizer alloc] init];
    _avSpeaker.delegate = self;
    //初始化要说出的内容
    AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString: text];
    //设置语速,语速介于AVSpeechUtteranceMaximumSpeechRate和AVSpeechUtteranceMinimumSpeechRate之间
    //AVSpeechUtteranceMaximumSpeechRate
    //AVSpeechUtteranceMinimumSpeechRate
    //AVSpeechUtteranceDefaultSpeechRate
    utterance.rate = 0.5;
    
    //设置音高,[0.5 - 2] 默认 = 1
    //AVSpeechUtteranceMaximumSpeechRate
    //AVSpeechUtteranceMinimumSpeechRate
    //AVSpeechUtteranceDefaultSpeechRate
    utterance.pitchMultiplier = 1;
    
    //设置音量,[0-1] 默认 = 1
    utterance.volume = 1;
    
    //读一段前的停顿时间
    utterance.preUtteranceDelay = 0;
    //读完一段后的停顿时间
    utterance.postUtteranceDelay = 1;
    
    //设置声音,是AVSpeechSynthesisVoice对象
    //AVSpeechSynthesisVoice定义了一系列的声音, 主要是不同的语言和地区.
    //voiceWithLanguage: 根据制定的语言, 获得一个声音.
    //speechVoices: 获得当前设备支持的声音
    //currentLanguageCode: 获得当前声音的语言字符串, 比如”ZH-cn”
    //language: 获得当前的语言
    //通过特定的语言获得声音
    AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
    //通过voicce标示获得声音
    //AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithIdentifier:AVSpeechSynthesisVoiceIdentifierAlex];
    utterance.voice = voice;
    
//    AudioSessionInitialize(NULL, NULL, nil, (__bridge void*) self);

    
    //开始朗读
    [_avSpeaker speakUtterance:utterance];
}

- (void)stopSpeech {
    if (_avSpeaker) {
        [_avSpeaker stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
    }
}


// 扬声器模式
- (void)setAudioWaiFangSession
{
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    //设置为播放
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];

    [audioSession setActive:YES error:nil];
}


#pragma mark - AVSpeechSynthesizerDelegate
//已经开始
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance{
    
    
}
//已经说完
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance{
    
    //如果朗读要循环朗读，可以在这里再次调用朗读方法
    //[_avSpeaker speakUtterance:utterance];
}
//已经暂停
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance{
    
}
//已经继续说话
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance{
    
}
//已经取消说话
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance{
    
}
//将要说某段话
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance{
    
} 


@end
